package com.sui.bigdata.sml.web.service.impl;

import com.sui.bigdata.sml.web.dto.ExperimentDTO;
import com.sui.bigdata.sml.web.dto.NodeGroupDTO;
import com.sui.bigdata.sml.web.exception.EntityNotFoundException;
import com.sui.bigdata.sml.web.repository.mapper.NodeGroupMapper;
import com.sui.bigdata.sml.web.repository.model.NodeGroup;
import com.sui.bigdata.sml.web.service.NodeGroupService;
import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author songhaicheng
 * @date 2019/11/7 15:02
 * @description
 * @reviewer
 */
@Service
@DependsOn("disconfConfig")
@Slf4j
public class NodeGroupServiceImpl implements NodeGroupService {

    @Autowired
    private NodeGroupMapper mapper;
    @Autowired
    private MapperFacade mapperFacade;

    @Override
    public Long add(NodeGroupDTO dto) {
        NodeGroup group = mapperFacade.map(dto, NodeGroup.class);
        mapper.insert(group);
        return group.getId();
    }

    @Override
    public NodeGroupDTO get(Long id) {
        NodeGroup group = mapper.get(id);
        if (group == null) {
            throw new EntityNotFoundException(NodeGroup.class, id);
        }
        return mapperFacade.map(group, NodeGroupDTO.class);
    }

    @Override
    public Boolean update(NodeGroupDTO dto) {
        get(dto.getId());
        NodeGroup group = mapperFacade.map(dto, NodeGroup.class);
        return mapper.update(group) == 1;
    }

    @Override
    public Boolean delete(Long id) {
        get(id);
        return mapper.delete(id) == 1;
    }

    @Override
    public List<NodeGroupDTO> listByExperiment(ExperimentDTO experimentDto) {
        return mapperFacade.mapAsList(mapper.listByExperimentId(experimentDto.getId()), NodeGroupDTO.class);
    }

    @Override
    public List<NodeGroupDTO> listOngoingsByExperiment(ExperimentDTO experimentDto) {
        return mapperFacade.mapAsList(mapper.listOngoingsByExperimentId(experimentDto.getId()), NodeGroupDTO.class);
    }

}
